# Virtual Router Simulator 
#
# Copyright (C) 2009-2010 VRSim Project
# Author: Choonho Son <choonho@kt.com>
# URL: <http://vrsim.googlecode.com>
# For license information, see LICENSE.TXT
#

from PyQt4 import QtGui,  QtCore
import VRSim.Globals as globals
from VRSim.Utils import debug
from VRSim.Ui.Ui_Login import Ui_Login

class Login(QtGui.QDialog,  Ui_Login):
    def __init__(self):
        QtGui.QDialog.__init__(self)
        Ui_Login.__init__(self)
        self.setupUi(self)
        # window title
        title = "KT-VNS (Version %s)" % globals.STRVER
        self.setWindowTitle(title)
        self.connect(self.pushButton_Login,  QtCore.SIGNAL("clicked()"),  self.actionLogin)
        
    def actionLogin(self):
        globals.GApp.userid = str(self.lineEdit_ID.text())
        globals.GApp.passwd = str(self.lineEdit_PW.text())
        
        msg = "actionLogin: id(%s) pw(%s)" % (globals.GApp.userid,  globals.GApp.passwd)
        globals.GApp.my_logger.debug(msg)
        
        # update VMAPI
        self.hide()
        
        # TODO: splash
        pic = QtGui.QPixmap(":/symbols/loading_6.png")
        splash = QtGui.QSplashScreen(pic,  QtCore.Qt.WindowStaysOnTopHint)
        splash.showMessage("Loading KTNS........",  QtCore.Qt.AlignBottom | QtCore.Qt.yellow)
        splash.show()
        globals.GApp.processEvents()
            
        globals.GApp.topology.updateVMAPI()
        msg = "updateVMAPI is finished"
        globals.GApp.my_logger.debug(msg)
        tf = globals.GApp.authenticated
        if tf == True:
            # enable MainWindow
            globals.GApp.mainWindow.setEnabled(globals.GApp.authenticated)
            globals.GApp.mainWindow.show()
            # init default information
            # splashscreen
            """
            pic = QtGui.QPixmap("/home/sunshout/VRSim-0.1/src/VRSim/Ui/splash.png")
            splash = QtGui.QSplashScreen(pic)
            splash.showMessage("Loading KTNS........",  QtCore.Qt.AlignBottom | QtCore.Qt.yellow)
            splash.show()
            globals.GApp.processEvents()
            """
            
            globals.GApp.topology.init()
            
            # log
            msg = globals.eventMsg["login"]
            nmsg = msg % globals.GApp.userid
            globals.GApp.mainWindow.NodesLog.addEvent(nmsg)
            
            globals.GApp.workspace.status_userid.setText(globals.GApp.userid)
            # everything is done
            # close windows
            self.close()
            
        else:
            self.show()

         
